package com.shanlitech.et.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;

@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = {"com.shanlitech.et.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfig {
    @Bean(name="dataSource")
    public DataSource dataSource(@Value("${mysql.driver}") String jdbcDriver,
                                 @Value("${mysql.url}") String jdbcUrl,
                                 @Value("${mysql.username}") String jdbcUsername,
                                 @Value("${mysql.password}") String jdbcPassword) {
        DruidDataSource source = new DruidDataSource();
        source.setDriverClassName(jdbcDriver);
        source.setUrl(jdbcUrl);
        source.setUsername(jdbcUsername);
        source.setPassword(jdbcPassword);
        //source.setInitialSize(10);
        //source.setMinIdle(10);
        //source.setMaxActive(100);
        //source.setMaxWait(60*1000);
        source.setTimeBetweenEvictionRunsMillis(60*1000);
        source.setMinEvictableIdleTimeMillis(300000);
        source.setValidationQuery("select 1");
        source.setTestWhileIdle(true);
        source.setTestOnBorrow(false);
        source.setTestOnReturn(false);
        source.setPoolPreparedStatements(true);
        source.setMaxPoolPreparedStatementPerConnectionSize(20);
        source.setKeepAlive(true);
        try {
            source.setFilters("stat,wall");
            source.addConnectionProperty("druid.stat.mergeSql", "true");
            source.addConnectionProperty("druid.stat.slowSqlMillis", "1000");
            source.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return source;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource")DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean(name="sqlSessionFactory")
    public FactoryBean<SqlSessionFactory> sqlSessionFactory(@Qualifier("dataSource")DataSource source) throws IOException {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(source);

        //sqlSessionFactory.setTypeAliasesPackage("com.harmazing.tc.manager.model.*");
        sqlSessionFactory.setTypeAliasesPackage("com.shanlitech.et.model.*");

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(resolver.getResources("classpath:/mapper/*Mapper.xml"));
        sqlSessionFactory.setConfigLocation(new ClassPathResource("config/mybatis.xml"));

        return sqlSessionFactory;
    }

}
